package com.cai.kcsj.controller.pre;

import com.cai.kcsj.entity.Order;
import com.cai.kcsj.entity.User;
import com.cai.kcsj.service.OrderServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/1/5 14 : 32
 * @package：com.cai.kcsj.controller.pre
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderServer orderServer;

    @RequestMapping("/order")
    public String order(HttpServletRequest request,Model model,@RequestParam(defaultValue = "1") Integer current){
        User user = (User) request.getSession().getAttribute("user");
        int id = user.getId();
        int startpage = (current-1)*5;
        List<Order> order = orderServer.getOrder(id,startpage);
        int count = orderServer.orderCount(id);
        int page = count%5==0?count/5:count/5+1;
        model.addAttribute("orderList",order);
        model.addAttribute("page",page);
        model.addAttribute("current",current);
        return "order";
    }

    @RequestMapping("/deleteorder/{id}")
    public String deleteorder(@PathVariable("id") Integer id){
        orderServer.deleteorder(id);
        return "redirect:/order/order";
    }
}
